package anli;

import java.util.*;

/**
 * 从总的名单中 抽取 5 个同学
 */
public class MainRandomGetSome {
    public static void main(String[] args) {
        List<Student> allStus = getStuListByPath("all.txt");
        Set<Student> rs = new HashSet();
        while (true) {
            int index = (int) (Math.random() * allStus.size()); // 0 1 2 3 4
            rs.add(allStus.get(index));
            if(rs.size()>=5){
                break;
            }
        }
        for (Student stu : rs) {
            System.out.println(stu);
        }
    }

    /**
     * 从文件中读取Student对象，返回List 集合
     *
     * @param path
     * @return
     */
    private static List<Student> getStuListByPath(String path) {
        List<Student> allStudents = new ArrayList<>();
        Scanner sca4All = new Scanner(Thread.currentThread().getContextClassLoader().getResourceAsStream(path));
        while (sca4All.hasNextLine()) {
            String line = sca4All.nextLine();
            if (line != null && line.trim().length() > 0) {
                String[] stuArr = line.split(",");
                Student student = new Student(stuArr[0], stuArr[1]);
                allStudents.add(student);
            }
        }
        sca4All.close();
        return allStudents;
    }


}
